package com.zjh.photoshopstudy.app.user;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.app.register.RegisterActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RetrievePwdActivity extends AppCompatActivity {
    EditText username, pwd, confirmPwd, codeView;
    Button sendCodeBtn, submitBtn, verificationBtn;
    String code;
    String uname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve_pwd);
        username = findViewById(R.id.username_edit2);
        pwd = findViewById(R.id.password_edit2);
        confirmPwd = findViewById(R.id.confirm_pwd_edit2);
        codeView = findViewById(R.id.code);
        sendCodeBtn = findViewById(R.id.send_code);
        submitBtn = findViewById(R.id.login_btn2);
        verificationBtn = findViewById(R.id.verification);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_code:
                sendCode();
                break;
            case R.id.verification:
                verificationCode();
                break;
            case R.id.login_btn2:
                submit();
                break;
        }
    }

    private void verificationCode() {
        String str = codeView.getText().toString().trim();
        if (str.equals(code)) {
            confirmPwd.setEnabled(true);
            pwd.setEnabled(true);
            submitBtn.setEnabled(true);
            Toast.makeText(this, "验证成功！", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "验证失败，请重新输入！", Toast.LENGTH_SHORT).show();
        }
    }

    private void sendCode() {
        String name = username.getText().toString().trim();
        if (isEmail(name)) {
            username.setBackgroundResource(R.drawable.dialog_bg);
            UserHttp.sendCode(this, name);
        } else {
            Toast.makeText(this, "请输入正确邮箱！", Toast.LENGTH_SHORT).show();
            username.setBackgroundResource(R.drawable.dialog_bg2);
        }
    }

    public void sendCodeSuccess(String code, String uname) {
        this.code = code;
        this.uname = uname;
        codeView.setEnabled(true);
        verificationBtn.setEnabled(true);
        codeView.requestFocus();
        Toast.makeText(this, "请输入验证码！", Toast.LENGTH_SHORT).show();
    }

    private void submit() {
        String password = pwd.getText().toString().trim();
        String confirmPassword = confirmPwd.getText().toString().trim();
        if (!password.equals(confirmPassword)) {
            Toast.makeText(this, "两次输入密码不相同！", Toast.LENGTH_SHORT).show();
        } else {
            UserHttp.resetPassword(this, uname, password);
        }
    }

    public static boolean isEmail(String email) {
        if (null == email || "".equals(email)) return false;
        //Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
        Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
        Matcher m = p.matcher(email);
        return m.matches();
    }

    public void resetSuccess() {
        Toast.makeText(this, "重置成功！", Toast.LENGTH_SHORT).show();
        finish();
    }

    public void resetFail() {
        Toast.makeText(this, "重置失败！", Toast.LENGTH_SHORT).show();
    }
}
